package com.article.controller;

import com.article.common.ResultBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Controller
public class SystemController {

    /**
     * 登录的页面
     *
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login() {
        return "/login";
    }

    /**
     * 主页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "/index";
    }

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody upload(MultipartFile file, HttpServletRequest request){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());

            // uploads文件夹位置
            String rootPath = "G:\\upload\\";
            // 原始名称
            String originalFileName = file.getOriginalFilename();
            // 新文件名
            String newFileName = "sliver" + res + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 创建年月文件夹
            Calendar date = Calendar.getInstance();
            File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));

            // 新文件
            File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
            // 判断目标文件所在目录是否存在
            if( !newFile.getParentFile().exists()) {
                // 如果目标文件所在的目录不存在，则创建父目录
                newFile.getParentFile().mkdirs();
            }
            System.out.println(newFile);
            // 将内存中的数据写入磁盘
            file.transferTo(newFile);
            // 完整的url
            String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH)+1) + "/" + newFileName;
            return ResultBody.success("http://localhost:8080/uploads/"+fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResultBody.error("图片上传失败");
        }
    }

    @RequestMapping(value = "/wx/upload",method = RequestMethod.POST)
    @ResponseBody
    public String wxUpload(MultipartFile file, HttpServletRequest request){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());

            // uploads文件夹位置
            String rootPath = "G:\\upload\\";
            // 原始名称
            String originalFileName = file.getOriginalFilename();
            // 新文件名
            String newFileName = "sliver" + res + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 创建年月文件夹
            Calendar date = Calendar.getInstance();
            File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));

            // 新文件
            File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
            // 判断目标文件所在目录是否存在
            if( !newFile.getParentFile().exists()) {
                // 如果目标文件所在的目录不存在，则创建父目录
                newFile.getParentFile().mkdirs();
            }
            System.out.println(newFile);
            // 将内存中的数据写入磁盘
            file.transferTo(newFile);
            // 完整的url
            String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH)+1) + "/" + newFileName;
            return "http://localhost:8080/uploads/"+fileUrl;
        }catch (Exception e){
            e.printStackTrace();
            return "图片上传失败";
        }
    }
}
